package com.roomio.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

@NamedQueries(value={
		@NamedQuery(name="group.transaction.by.groupid",query="from GroupTransaction gt join gt.userTransactions ut where gt.group.id=:groupId order by ut.transactionDate ")
})
@Entity
@Table (name="GROUP_TRANSACTION")
public class GroupTransaction extends Transaction {	
	
	
	@ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.REFRESH })
	@JoinColumn(name = "GROUP_ID", nullable = false)
	private Group group;
	
	@OneToMany(mappedBy="groupTransaction", cascade = { CascadeType.ALL })
	private Set<UserTransaction> userTransactions= new HashSet<UserTransaction>();

	public Group getGroup() {
		return this.group;
	}

	public void setGroup(Group groupId) {
		this.group = groupId;
	}
	

	public Set<UserTransaction> getUserTransactions() {
		return userTransactions;
	}

	public void setUserTransactions(Set<UserTransaction> userTransactions) {
		this.userTransactions = userTransactions;
	}
	
	public void addUserTransaction(UserTransaction userTransaction){
		userTransactions.add(userTransaction);
		userTransaction.setGroupTransaction(this);
	}

	public String toString() {
		return ReflectionToStringBuilder.toString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

}
